﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CheckResults = LabChecker.Models.CheckResults;

namespace LabChecker.ViewModels
{
    public class CheckResultViewModel
    {
        public CheckResultViewModel(CheckResults.TaskBase taskCheckResult)
        {
            if (taskCheckResult is CheckResults.Task.Accepted)
            {
                Result = "Accepted";
            }
            else if (taskCheckResult is CheckResults.Task.CompilationError)
            {
                var compilationErrorResult = taskCheckResult as CheckResults.Task.CompilationError;
                Result = "Compilation Error";
                Message = String.Format("Line {0} Col {1}: {2}",
                    compilationErrorResult.Line,
                    compilationErrorResult.Column,
                    compilationErrorResult.ErrorMessage);
            }
            else if (taskCheckResult is CheckResults.Task.FailedTest.RuntimeError)
            {
                var runtimeErrorResult = taskCheckResult as CheckResults.Task.FailedTest.RuntimeError;
                TestCaseNumber = runtimeErrorResult.Number;
                Result = "Runtime Error";

                if (runtimeErrorResult.Line == 0)
                    Message = String.Format("Input:\n{0}\n\nError: {1}",
                    runtimeErrorResult.Input,
                    runtimeErrorResult.Message);
                else
                    Message = String.Format("Input:\n{0}\n\n{1} at {2}:{3}",
                        runtimeErrorResult.Input,
                        runtimeErrorResult.Message, runtimeErrorResult.Line, runtimeErrorResult.Column);
            }
            else if (taskCheckResult is CheckResults.Task.FailedTest.TimeLimitExceeded)
            {
                var timeLimitErrorResult = taskCheckResult as CheckResults.Task.FailedTest.TimeLimitExceeded;
                TestCaseNumber = timeLimitErrorResult.Number;
                Result = "Time limit exceeded";
                Message = String.Format("Your program runs too slow. Try to make it faster", timeLimitErrorResult.TimeLimit);
            }
            else if (taskCheckResult is CheckResults.Task.FailedTest.InvalidAction)
            {
                var invalidActionResult = taskCheckResult as CheckResults.Task.FailedTest.InvalidAction;
                Result = "Invalid action";
                TestCaseNumber = invalidActionResult.Number;
                Message = String.Format("Your program was trying to perform an illegal action");
            }
            else if (taskCheckResult is CheckResults.Task.FailedTest.WrongAnswer)
            {
                var wrongAnswerResult = taskCheckResult as CheckResults.Task.FailedTest.WrongAnswer;
                TestCaseNumber = wrongAnswerResult.Number;

                var testCaseCheckResult = wrongAnswerResult.TestCaseCheckResult;

                if (testCaseCheckResult is CheckResults.TestCase.WrongAnswer)
                {
                    var wrongAnswerTestCaseCheckResult = testCaseCheckResult as CheckResults.TestCase.WrongAnswer;
                    Result = "Wrong Answer";
                    Message = "Input:\n" + wrongAnswerResult.Input + "\n\nОжидаемый вывод:\n" + testCaseCheckResult.Answer + "\n\nВывод программы:\n" + testCaseCheckResult.Output;
                }
            }
        }

        public int? TestCaseNumber { get; private set; }
        public string Result { get; private set; }
        public string Message { get; set; }

        public CheckResultViewModel()
        {

        }
    }
}